package controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import entity.Backend_user;
import service.Backend_userService;
@Controller
@RequestMapping("/backend_user")
public class Backend_userController {

	@Autowired
	private Backend_userService userService;
	
	/**
	 * 登录
	 */
	@RequestMapping("/login")
	public String login(@RequestParam(value="userCode",required=false) String userCode,
						@RequestParam(value="userPassword",required=false) String userPassword,
						HttpServletRequest request){
		
		Backend_user user = new Backend_user();
		user = userService.queryUserCode(userCode);
		Backend_user session = (Backend_user) request.getSession().getAttribute("userSession");
		
		if(session!=null) {
			return "backend/main";
		}
		if(user == null) {
			request.setAttribute("error", "用户名错误，请重试");
			return "backendlogin";
		} else {
			user = userService.loginBackend(userCode, userPassword);
			if(user == null) {
				request.setAttribute("error", "密码错误，请重试");
				return "backendlogin";
			}
			request.getSession().setAttribute("userSession", user);
		}
		return "redirect:/backend_user/jlogin";
	}
	
	/**
	 * 判断Session是否为空，为空则拦截
	 */
	@RequestMapping("jlogin")
	public String jlogin(HttpServletRequest request) {
	
			return"backend/main";

	}
		
	/**
	 * 退出
	 */
	@RequestMapping("logout")
	public String logout(HttpServletRequest request) {
		request.getSession().removeAttribute("userSession");
		return "backendlogin";
	}
}
